#include <iostream>
#include "Buffer.hpp"
#include "Epoller.hpp"
#include <memory>

namespace Test
{
    template <class T>
    class shared_ptr
    {
    private:
        T *_ptr;      // 指向管理对象的指针
        int *_pcount; // 引用计数

    public:
        shared_ptr(T *ptr = nullptr)
            : _ptr(ptr), _pcount(new int(1)) {}
        ~shared_ptr() { release(); }

        // 释放资源的函数
        void release()
        {
            if (--(*_pcount) == 0)
            {
                delete _ptr;
                delete _pcount;
                _ptr = nullptr;
                _pcount = nullptr;
            }
        }

        T &operator*() { return *_ptr; }
        T *operator->() { return _ptr; }

        // 拷贝构造和赋值运算符重载函数
        shared_ptr(const shared_ptr<T> &sp)
            : _ptr(sp._ptr), _pcount(sp._pcount) { ++(*_pcount); }
        shared_ptr<T> operator=(const shared_ptr<T> &sp)
        {
            if (_ptr != sp._ptr)
            {
                release();

                _ptr = sp._ptr;
                _pcount = sp._pcount;
                ++(*_pcount);
            }

            return *this;
        }
    };

    template <class T>
    class weak_ptr
    {
    private:
        T *_ptr = nullptr;

    public:
        weak_ptr() {}
        weak_ptr(const shared_ptr<T> &sp)
            : _ptr(sp.get())
        {
        }

        weak_ptr<T> &operator=(const shared_ptr<T> &sp)
        {
            _ptr = sp.get();
            return *this;
        }
    };
}